//
//  SunBankManagerVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/27.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunBankManagerVC.h"
#import "SunBankCardCell.h"
#import "SunDelegateWebVC.h"
#import "SunOpenBindVC.h"
#import "ChangeBankCardVC.h"

@interface SunBankManagerVC ()
{
    UILabel *_imgBank1;
    UILabel *_imgBank2;
    UILabel *_imgCard1;
    UILabel *_imgCard2;
    UILabel *_name;
    UILabel *_jxAccount;
    UILabel *_jxBank;
    UILabel *_bank;
    UIButton *_btn;
}
@property (nonatomic, strong) UIImageView *moreView;
@end

@implementation SunBankManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的银行卡";
    self.view.backgroundColor = BACKGROUND;
    [self creatUI];
    [self updateViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestToUser];
}

- (void)creatUI{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, X(25), 25)];
    [btn setImage:[UIImage imageNamed:@"third_bank_more"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = barbutton;
#pragma mark- 卡片界面
    UIImageView *bg = [[UIImageView alloc]initWithFrame:CGRectMake(X(15), X(14), X(345), X(187))];
    bg.layer.cornerRadius = 5;
    [self.view addSubview:bg];
    bg.image = [UIImage imageNamed:@"third_bank_bg"];
    bg.clipsToBounds = YES;
    UIImageView *logo = [[UIImageView alloc]init];
    [bg addSubview:logo];
    [logo lt_setSize:CGSizeMake(X(30), X(30)) save:YES];
    [logo lt_setTop:X(15) to:nil save:YES];
    [logo lt_setLeft:X(10) to:nil save:YES];
    UILabel *title = [[UILabel alloc]init];
    [bg addSubview:title];
    [title lt_YcenterTo:logo save:YES];
    [title lt_setLeft:X(9) to:logo save:YES];
    title.textColor = WHITE;
    title.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    UILabel *bankNum = [[UILabel alloc]init];
    [bg addSubview:bankNum];
    [bankNum lt_XcenterTo:nil save:YES];
    [bankNum lt_setTop:X(26) to:title save:YES];
    bankNum.textColor = COLOR_TITLE_MAIN;
    bankNum.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18];
    UILabel *numWord = [[UILabel alloc]init];
    [bg addSubview:numWord];
    [numWord lt_setLeft:X(9) to:nil save:YES];
    [numWord lt_setBottom:X(11) to:nil save:YES];
    numWord.textColor = COLOR_TITLE_MAIN;
    numWord.font = [UIFont systemFontOfSize:12];
    numWord.text = @"预留手机号";
    UILabel *phoneNum = [[UILabel alloc]init];
    [bg addSubview:phoneNum];
    [phoneNum lt_setRight:X(9) to:nil save:YES];
    [phoneNum lt_setBottom:X(11) to:nil save:YES];
    phoneNum.textColor = COLOR_TITLE_MAIN;
    phoneNum.font = [UIFont systemFontOfSize:12];
    NSDictionary *dic = [Sun getUser:USER];
    title.text = [NSString stringWithFormat:@"%@(尾号%@)",[dic objectForKey:@"bank"],[[dic objectForKey:@"bankCard"]substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
    phoneNum.text = [NSString stringWithFormat:@"%@****%@",[[dic objectForKey:@"phone"]substringToIndex:3],[[dic objectForKey:@"phone"]substringFromIndex:7]];
    bankNum.text = [NSString stringWithFormat:@"***  ****  ****  %@",[[dic objectForKey:@"bankCard"]substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
    [logo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[dic objectForKey:@"bankImg"]]]];
    UIImageView *defautImage = [[UIImageView alloc]init];
    [bg addSubview:defautImage];
    [defautImage lt_setSize:CGSizeMake(X(30), Y(25)) save:YES];
    [defautImage lt_topEqual:nil save:YES];
    [defautImage lt_setRight:X(15) to:nil save:YES];
    defautImage.image = [UIImage imageNamed:@"third_bank_default"];
    
    UILabel *l1 = [[UILabel alloc]init];
    l1.tag = 1;
    [bg addSubview:l1];
    [l1 lt_XcenterTo:nil save:YES];
    [l1 lt_setTop:X(26) to:bankNum save:YES];
    l1.textColor = WHITE;
    l1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:10];
    NSString *perLim = [[dic objectForKey:@"perLimit"] integerValue]>=10000?[NSString stringWithFormat:@"%ld万",[[dic objectForKey:@"perLimit"] integerValue]/10000]:[NSString stringWithFormat:@"%ld",[[dic objectForKey:@"perLimit"] integerValue]];
    NSString *dayLim = [[dic objectForKey:@"dayLimit"] integerValue]>=10000?[NSString stringWithFormat:@"%ld万",[[dic objectForKey:@"dayLimit"] integerValue]/10000]:[NSString stringWithFormat:@"%ld",[[dic objectForKey:@"dayLimit"] integerValue]];
    l1.text = [NSString stringWithFormat:@"单笔限额%@元,单日限额%@元",perLim,dayLim];
   
    UILabel *label = [[UILabel alloc]init];
    [self.view addSubview:label];
    [label lt_setLeft:X(24) to:nil Right:X(24) to:nil save:YES];
    [label lt_setTop:X(11) to:bg save:YES];
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
    label.numberOfLines = 0;
    label.preferredMaxLayoutWidth = SCREENWIDTH - X(48);
    label.textColor = COLOR_TITLE_MINOR;
    label.text = [NSString stringWithFormat:@"为了您的账户安全，仅支持绑定一张银行卡(暂不支持信用卡)进行提现和充值，如有疑问，请联系客服%@",KEFU];
    
#pragma mark- 操作界面
    self.moreView = [[UIImageView alloc]init];
    [self.view addSubview:self.moreView];
    self.moreView.hidden = YES;
    [self.moreView lt_setTop:X(2) to:nil save:YES];
    [self.moreView lt_setRight:X(10) to:nil save:YES];
    [self.moreView lt_setSize:CGSizeMake(X(148), X(139)) save:YES];
    [self.moreView setImage:[UIImage imageNamed:@"third_bank_morebg"]];
    self.moreView.userInteractionEnabled = YES;
    //换卡
    UIButton *change = [[UIButton alloc]init];
    [self.moreView addSubview:change];
    [change lt_setSize:CGSizeMake(X(140), X(40)) save:YES];
    [change lt_XcenterTo:nil save:YES];
    [change lt_setTop:X(10) to:nil save:YES];
    change.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    [change setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
    [change setTitle:@"换绑银行卡" forState:UIControlStateNormal];
    [change addTarget:self action:@selector(changeBankCard) forControlEvents:UIControlEventTouchUpInside];
    
    _btn = [[UIButton alloc]init];
    [self.moreView addSubview:_btn];
    [_btn lt_setSize:CGSizeMake(X(140), X(40)) save:YES];
    [_btn lt_XcenterTo:nil save:YES];
    [_btn lt_setTop:X(1) to:change save:YES];
    [_btn setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
    _btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    [_btn addTarget:self action:@selector(changeBank) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *phone = [[UIButton alloc]init];
    [self.moreView addSubview:phone];
    [phone lt_setSize:CGSizeMake(X(140), X(40)) save:YES];
    [phone lt_XcenterTo:nil save:YES];
    [phone lt_setBottom:X(7) to:nil save:YES];
    phone.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
    [phone setTitleColor:COLOR_TITLE_MAIN forState:UIControlStateNormal];
    [phone setTitle:@"更新银行预留手机号" forState:UIControlStateNormal];
    [phone addTarget:self action:@selector(changePhone) forControlEvents:UIControlEventTouchUpInside];

    
}
- (void)changePhone {
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_CHANGEBANKPHONE];
    [[Sun shareSun]showActivity:self];
    SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
    NSDictionary *param = @{@"device":@"iOS",@"accessId":[Sun getUser:ACCESSID]};
    NSString *str = [Sun stringFromDictionary:(NSDictionary *)param];
    vc.params = str;
    vc.urlStr = url;
    vc.title = @"更新银行预留手机号";
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark- moreAction
- (void)moreAction{
    self.moreView.hidden = !self.moreView.hidden;
}
- (void)updateViews{
    NSDictionary *dic = [Sun getUser:USER];
    _imgBank1.text = @"存管账户卡片";
    _imgCard1.text = [NSString stringWithFormat:@"%@********%@",[[dic objectForKey:@"jxCard"] substringToIndex:4],[[dic objectForKey:@"jxCard"] substringFromIndex:[[dic objectForKey:@"jxCard"]length] - 4]];
    _name.text = [NSString stringWithFormat:@"开户姓名：%@",[dic objectForKey:@"name"]];
    _jxAccount.text = [NSString stringWithFormat:@"存管账户：%@",[Sun formatStringToBank:[dic objectForKey:@"jxCard"]]];
    _jxBank.text = [NSString stringWithFormat:@"账户开户行：%@",[dic objectForKey:@"jxBank"]];
    if ([[dic objectForKey:@"status"] intValue] == 3) {
        _imgCard2.hidden = NO;
        _imgBank2.text = @"绑定的银行卡";
        _imgBank2.textAlignment = NSTextAlignmentLeft;
        _imgCard2.text = [NSString stringWithFormat:@"%@********%@",[[dic objectForKey:@"bankCard"] substringToIndex:4],[[dic objectForKey:@"bankCard"] substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
        _bank.text = [NSString stringWithFormat:@"银行卡绑定：%@尾号%@",[dic objectForKey:@"bank"],[[dic objectForKey:@"bankCard"] substringFromIndex:[[dic objectForKey:@"bankCard"]length] - 4]];
        [_btn setTitle:@"解绑银行卡" forState:UIControlStateNormal];
    }else {
        self.navigationItem.rightBarButtonItem.accessibilityElementsHidden = YES;
        _imgCard2.hidden = YES;
        _imgBank2.text = @"未绑定银行卡";
        _imgBank2.textAlignment = NSTextAlignmentCenter;
        [_bank setMarkup:@"银行卡绑定：<font color=\"ff5971\">未绑定银行卡</font>"];
        [_btn setTitle:@"立即绑定" forState:UIControlStateNormal];
    }
}

- (void)requestToUser{
    NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:url withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
        [self updateViews];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}
- (void)changeBankCard {
    ChangeBankCardVC *changeVC = [[ChangeBankCardVC alloc]init];
    [self.navigationController pushViewController:changeVC animated:YES];
}
- (void)changeBank{
    NSDictionary *dic = [Sun getUser:USER];
    if ([[dic objectForKey:@"status"] intValue] == 3){
        [[Sun shareSun]alertAlign:NSTextAlignmentCenter title:@"温馨提示" content:@"当可用余额为0，且没有未结清的标的时，可解绑银行卡，您确定要解绑银行卡？" left:@"确定" right:@"取消" cancel:^{
            NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_UNBIND];
            [[Sun shareSun]showActivity:self];
            SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
            NSDictionary *param = @{@"device":@"iOS",@"accessId":[Sun getUser:ACCESSID]};
            NSString *str = [Sun stringFromDictionary:(NSDictionary *)param];
            vc.params = str;
            vc.urlStr = url;
            vc.title = @"更新银行预留手机号";
            [self.navigationController pushViewController:vc animated:YES];
            } sure:^{
            [[Sun shareSun]hideAll];
        } To:self];
    }else {
        SunOpenBindVC *vc = [[SunOpenBindVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
